import { AnimatedSprite, Application, Assets } from 'pixi.js';

export default function usePiXiFrameAni(props: {
  imagePaths: string[];
  container: string;
}) {


  async function init() {
    if(!props.container) return
    const container = document.getElementById(props.container as string) as HTMLDivElement

    const app = new Application();
    await app.init({ resizeTo: container });
    container.appendChild(app.canvas);

    const texturePromises = props.imagePaths.map((path) => Assets.load(path));
    return Promise.all(texturePromises).then((textures) => {

      // 创建动画精灵
      const animatedSprite = new AnimatedSprite(textures);
      // 设置动画参数

      animatedSprite.width = app.screen.width;
      animatedSprite.height = app.screen.height;
      animatedSprite.animationSpeed = 0.3; // 调整帧率
      animatedSprite.loop = false;
      // 添加到舞台
      app.stage.addChild(animatedSprite);
      // 播放动画
      animatedSprite.play();
    });
  }

  return {
    init,
  };
}
